package com.tiantianyuegou.fragment;


import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.tiantianyuegou.R;
import com.tiantianyuegou.activity.AddressActivity;
import com.tiantianyuegou.activity.ISellerActivity;
import com.tiantianyuegou.activity.MemberDataActivity;
import com.tiantianyuegou.activity.MyCollectionActivity;
import com.tiantianyuegou.activity.MyCouponActivity;
import com.tiantianyuegou.activity.MyNewsActivity;
import com.tiantianyuegou.activity.MyOneGouActivity;
import com.tiantianyuegou.activity.MyOrderActivity;
import com.tiantianyuegou.activity.MySecurityCentreActivity;
import com.tiantianyuegou.activity.SelectBankActivity;
import com.tiantianyuegou.activity.SetActivity;
import com.tiantianyuegou.constant.Constant;
import com.tiantianyuegou.http.ApiDefine;
import com.tiantianyuegou.http.SPHelper;
import com.tiantianyuegou.model.UserInfoModel;
import com.tiantianyuegou.util.LoadingDialog;

import org.json.JSONObject;

/**
 * Created by apple on 16/1/4.
 * 我的
 */
public class MemberFragment extends Fragment implements View.OnClickListener {

    private TextView member_name;
    private TextView txt_guan_num, txt_packets_num, txt_news_num, txt_discount_num;
    private LinearLayout linear_myguan, linear_packets, linear_mynews, linear_my_discount;
    private LinearLayout linear_myOrder,linear_myOnegou, linear_myCollection, linear_security,
            linear_address, linear_my_bankcard, linear_business;
    private ImageView img_head, img_set;

    private String tmName,tmId, tmType, tmProv, tmCity,tmAddress,tmRegion,tmPhone,tmAmount,
            tmScore,tmRecommend,tmRecommendCode;
    private String tmGuanCount,tmRedCount,tmBalance;

    private LoadingDialog loadingDialog;


    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.fragment_member, container, false);
        init(v);
        return v;
    }

//    @Override
//    public void onCreate(@Nullable Bundle savedInstanceState) {
//        super.onCreate(savedInstanceState);
//        loadMember();
//        Log.e("MemberFragment", "MemberFragment onCreate");
//    }

    @Override
    public void onResume() {
        super.onResume();
        loadingDialog=new LoadingDialog(MemberFragment.this.getContext());
        loadingDialog.setMsg("请稍等");
        loadMember();

    }

    private void loadMember() {
        loadingDialog.showDialog();
        RequestParams params = new RequestParams();
        params.addBodyParameter("tmId", SPHelper.getTmId());
        Log.e("MemberFragment", "MemberFragment loadMember" + SPHelper.getTmId());
        loadData(HttpRequest.HttpMethod.POST, ApiDefine.USER_INFO, params, new RequestCallBack<String>() {
            @Override
            public void onSuccess(ResponseInfo<String> responseInfo) {
                loadingDialog.dismiss();
                Log.e("MemberFragment", "onSuccess response.result  "
                        + responseInfo.result);
                String returnstr = responseInfo.result;

                try {
                    JSONObject json = new JSONObject(returnstr);
                    String code = json.getString("code");
                    Log.e("MemberFragment", json.getString("message"));
//                    Toast.makeText(MemberFragment.this.getContext(), json.getString("message"), Toast.LENGTH_LONG).show();
                    if (code.equals("100")) {
                        JSONObject jsonObject = json.getJSONObject("info");
                        member_name.setText(jsonObject.getString("tm_name"));
                        txt_guan_num.setText(jsonObject.getString("tm_score"));
                        txt_packets_num.setText(jsonObject.getString("tm_amount"));
                        txt_discount_num.setText(jsonObject.getString("tm_recommend_code"));
//                        txt_news_num.setText(jsonObject.getString("tm_type"));
//                        txt_guan_num.setText(jsonObject.getString("tmGuanCount"));
//                        txt_packets_num.setText(jsonObject.getString("tmRedCount"));
//                        txt_discount_num.setText(jsonObject.getString("ticket"));
//
//                        tmGuanCount=jsonObject.getString("tmGuanCount");
//                        tmRedCount=jsonObject.getString("tmRedCount");
//                        tmBalance=jsonObject.getString("tmBalance");


//                        message：请求结果描述
//                        tm_id：会员id
//                        tm_name：会员名称
//                        tm_type：会员类型 1：普通会员 2：商户 3：业务员 4：区域经理
//                        tm_photo：头像
//                        tm_prov_id：省id
//                        tm_city_id：市id
//                        tm_region_id：区id
//                        tm_address：地址
//                        tm_mobile：手机号码
//                        tm_amount：余额
//                        tm_score：积分
//
//                        tm_recommend_code：邀请码
//
//                        RecommendCode :推荐人邀请码
                        tmName = jsonObject.getString("tm_name");
                        tmPhone = jsonObject.getString("tm_photo");
                        tmId = jsonObject.getString("tm_id");
                        tmType = jsonObject.getString(" tm_type");
                        tmProv = jsonObject.getString("tm_prov_id");
                        tmCity = jsonObject.getString("tm_city_id");
                        tmAddress = jsonObject.getString("tm_address");
                        tmRegion = jsonObject.getString("tm_region_id");
                        tmPhone = jsonObject.getString("tm_mobile");
                        tmAmount = jsonObject.getString("tm_amount");
                        tmScore = jsonObject.getString("tm_score");
                        tmRecommend = jsonObject.getString("tm_recommend_code");
                        tmRecommendCode = jsonObject.getString("RecommendCode");




//                                tmIdcard = jsonObject.getString("tmIdcard");
//                        tmEmail = jsonObject.getString("tmEmail");
//                        tmAvatar=jsonObject.getString("tmAvatar");

//                        new UserInfoModel().setTmAvatar(jsonObject.getString("tmAvatar"));

                        SPHelper.setTmGuanCount(jsonObject.getString("tmGuanCount"));
                        SPHelper.setTmRedCount(jsonObject.getString("tmRedCount"));

                        Log.e("THIS",SPHelper.getTmGuanCount());
                        Log.e("THIS",SPHelper.getTmRedCount());

                        ImageLoader.getInstance().displayImage(jsonObject.getString("tmAvatar"), img_head);


                        // jsonObject.getString("");
                        Log.e("MemberFragment", "if code == 100" + tmName + "   " + tmPhone);   //+new UserInfoModel().getTmAvatar());

                    } else {
                        tmName = "";
                        tmPhone = "";
//                        new UserInfoModel().setTmAvatar("");

                        member_name.setText("");
                        txt_news_num.setText("");
                        txt_guan_num.setText("");
                        txt_packets_num.setText("");
                        txt_discount_num.setText("");

                        Log.e("MemberFragment", "else code ！= 100");
                    }
                } catch (Exception e) {
                    loadingDialog.dismiss();
                    e.printStackTrace();

                    Log.e("MemberFragment", "MemberFragment try catch"+e.toString());
                }
            }

            @Override
            public void onFailure(HttpException e, String s) {
                loadingDialog.dismiss();
                Log.e("TAG", "error" + s);
                Log.e("MemberFragment", "MemberFragment onFailure");

            }
        });
    }

    private void init(View view) {
        if (view != null) {

            member_name = (TextView) view.findViewById(R.id.txt_member_name);

            img_head = (ImageView) view.findViewById(R.id.img_head_p);
            img_set = (ImageView) view.findViewById(R.id.img_set);

            txt_guan_num = (TextView) view.findViewById(R.id.txt_guan_num);
            txt_packets_num = (TextView) view.findViewById(R.id.txt_packets_num);
            txt_news_num = (TextView) view.findViewById(R.id.txt_news_num);
            txt_discount_num = (TextView) view.findViewById(R.id.txt_discount_num);

            linear_myguan = (LinearLayout) view.findViewById(R.id.linear_myguan);
            linear_packets = (LinearLayout) view.findViewById(R.id.linear_packets);
            linear_mynews = (LinearLayout) view.findViewById(R.id.linear_mynews);
            linear_my_discount = (LinearLayout) view.findViewById(R.id.linear_my_discount);

            linear_myOrder = (LinearLayout) view.findViewById(R.id.linear_myOrder);
            linear_myOnegou = (LinearLayout) view.findViewById(R.id.linear_myOnegou);
            linear_myCollection = (LinearLayout) view.findViewById(R.id.linear_myCollection);
            linear_security = (LinearLayout) view.findViewById(R.id.linear_security);
            linear_address = (LinearLayout) view.findViewById(R.id.linear_address);
            linear_my_bankcard = (LinearLayout) view.findViewById(R.id.linear_my_bankcard);
            linear_business = (LinearLayout) view.findViewById(R.id.linear_business);



            img_head.setOnClickListener(this);
            img_set.setOnClickListener(this);

            linear_myguan.setOnClickListener(this);
            linear_packets.setOnClickListener(this);
            linear_mynews.setOnClickListener(this);
            linear_my_discount.setOnClickListener(this);
            linear_myOrder.setOnClickListener(this);
            linear_myOnegou.setOnClickListener(this);
            linear_myCollection.setOnClickListener(this);
            linear_security.setOnClickListener(this);
            linear_address.setOnClickListener(this);
            linear_my_bankcard.setOnClickListener(this);
            linear_business.setOnClickListener(this);
        }
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {

            case R.id.img_head_p:
                Intent intent1 = new Intent();
                Log.e("onClick",tmName+"   "+tmPhone);
                intent1.putExtra("tmName",tmName);
                intent1.putExtra("tmPhone",tmPhone);
//                intent1.putExtra("tmIdcard",tmIdcard);
//                intent1.putExtra("tmEmail",tmEmail);
//                intent1.putExtra("tmAvatar",tmAvatar);
                intent1.setClass(MemberFragment.this.getContext(), MemberDataActivity.class);
                startActivity(intent1);
                break;

            case R.id.img_set:
                Intent intent2 = new Intent();
                intent2.setClass(MemberFragment.this.getContext(), SetActivity.class);
                startActivity(intent2);
                break;
            case R.id.linear_myOnegou:
                Intent linear_myOnegou = new Intent();

                linear_myOnegou.setClass(MemberFragment.this.getContext(), MyOneGouActivity.class);
                linear_myOnegou.putExtra("type","member");
                startActivity(linear_myOnegou);

                break;

            case R.id.linear_myguan:
                Intent linear_myguan = new Intent();
                linear_myguan.putExtra("tmGuanCount",tmGuanCount);
//                linear_myguan.setClass(MemberFragment.this.getContext(), MyGuanActivity.class);
                startActivity(linear_myguan);
                break;

            case R.id.linear_packets:
                Intent linear_packets = new Intent();
                Constant.REDCOUNT=tmRedCount;
                Constant.BLANCE=tmBalance;
//                linear_packets.putExtra("tmRedCount",tmRedCount);
//                linear_packets.putExtra("tmBalance",tmBalance);
//                linear_packets.setClass(MemberFragment.this.getContext(), MyRedPackageActivity.class);
                startActivity(linear_packets);
                break;

            case R.id.linear_mynews:
                Intent linear_mynews = new Intent();
                linear_mynews.setClass(MemberFragment.this.getContext(), MyNewsActivity.class);
                startActivity(linear_mynews);
                break;

            case R.id.linear_my_discount:
                Intent linear_my_discount = new Intent();
                linear_my_discount.setClass(MemberFragment.this.getContext(), MyCouponActivity.class);
                startActivity(linear_my_discount);
                break;

            case R.id.linear_myOrder:
                Intent linear_myorder = new Intent();
                linear_myorder.setClass(MemberFragment.this.getContext(), MyOrderActivity.class);
                linear_myorder.putExtra("type","member");
                startActivity(linear_myorder);
                break;

            case R.id.linear_myCollection:
                Intent linear_myCollection = new Intent();
                linear_myCollection.setClass(MemberFragment.this.getContext(), MyCollectionActivity.class);
                startActivity(linear_myCollection);
                break;

            case R.id.linear_security:
                Intent linear_security = new Intent();
                linear_security.setClass(MemberFragment.this.getContext(), MySecurityCentreActivity.class);
                startActivity(linear_security);
                break;

            case R.id.linear_address:
                Intent linear_address = new Intent();
                linear_address.setClass(getActivity(), AddressActivity.class);
                startActivity(linear_address);
                break;

            case R.id.linear_my_bankcard:
                Intent linear_my_bankcard = new Intent();
                linear_my_bankcard.putExtra("type", "2");
                linear_my_bankcard.setClass(MemberFragment.this.getContext(), SelectBankActivity.class);
                startActivityForResult(linear_my_bankcard, 2);
                break;

            case R.id.linear_business:

                Intent linear_business = new Intent();
                linear_business.setClass(MemberFragment.this.getContext(), ISellerActivity.class);
                startActivity(linear_business);
                break;

            default:
                break;
        }
    }



    public void loadData(HttpRequest.HttpMethod method, String url, RequestParams params,
                         RequestCallBack<String> callBack) {
        HttpUtils httpUtils = new HttpUtils();
        // 30秒超时
        httpUtils.configCurrentHttpCacheExpiry(1000 * 30);
        if (params == null) {
            params = new RequestParams();
        } else {
            params.addBodyParameter("key", Constant.key);
            params.addBodyParameter("code", Constant.code);
        }
        Log.e("MemberFragment", "send post request");
        httpUtils.send(method, url, params, callBack);
    }
}
